//
// MaFenetre.cpp for  in /home/goldbe_y//cpp/plazza-2014-bramar_g/Qt
//
// Made by yoni goldberg
// Login   <goldbe_y@epitech.net>
//
// Started on  Sun Apr 17 16:46:17 2011 yoni goldberg
// Last update Sat May 28 18:48:51 2011 leo faucon
//

#include "MaFenetre.hpp"
#include "convert.hpp"

MaFenetre::MaFenetre(QApplication &app)
{
  (void)app;

  Parseur = NULL;
  math = NULL;
  math2 = NULL;

  setGeometry(250, 250, 400, 400);
  Bienvenue = new QLabel("");
  Consigne = new QLabel("Charger un fichier puis appuyer sur 'Calculer' : ");
  Quit = new QPushButton("Quit");
  calculer = new QPushButton("Calculer");
  charger = new QPushButton("Charger");
  graph = new QPushButton("Graphique");


  reg1 = new QLabel("Regression de Y sur X :");
  reg2 = new QLabel("Regression de X sur Y :");
  coef = new QLabel("Coefficient de correlation :");

  for (int i = 0; i < 3; i++)
    type1 << new QLabel("");
  for (int i = 0; i < 3; i++)
    type1[i]->setText(QString(tab[i].c_str()));
  for (int i = 0; i < 3; i++)
    value1 << new QLabel("");


  for (int i = 0; i < 3; i++)
    type2 << new QLabel("");
  for (int i = 0; i < 3; i++)
    type2[i]->setText(QString(tab[i].c_str()));
  for (int i = 0; i < 3; i++)
    value2 << new QLabel("");

  type3 = new QLabel(QString(tab[3].c_str()));
  value3 = new QLabel("");

  layout = new QGridLayout;

  Place(app);
}

void		MaFenetre::Place(QApplication &app)
{
  layout->addWidget(Bienvenue, 0, 1);


  layout->addWidget(reg1,2,0,1,2);
  int	index_aff = 3;
  for (int i = 0; i < 3; i++)
    {
      layout->addWidget(type1[i],index_aff, 0);
      index_aff++;
    }
  index_aff = 3;
  for (int i = 0; i < 3; i++)
    {
      layout->addWidget(value1[i],index_aff, 1);
      index_aff++;
    }

  layout->addWidget(reg2,6,0,1,2);
  index_aff = 7;
  for (int i = 0; i < 3; i++)
    {
      layout->addWidget(type2[i],index_aff, 0);
      index_aff++;
    }
  index_aff = 7;
  for (int i = 0; i < 3; i++)
    {
      layout->addWidget(value2[i],index_aff, 1);
      index_aff++;
    }

  layout->addWidget(coef,10,0,1,2);
  layout->addWidget(type3,11,0);
  layout->addWidget(value3,11,1);

  layout->addWidget(graph, 12, 0);
  layout->addWidget(calculer, 12, 1);
  layout->addWidget(Consigne, 0, 0, 1, 2, Qt::AlignHCenter);
  layout->addWidget(charger, 1, 0, 1, 2);
  layout->addWidget(Quit, 13, 0, 1, 2);
  QWidget::connect(Quit, SIGNAL(clicked()), &app, SLOT(quit()));
  QWidget::connect(calculer, SIGNAL(clicked()), this, SLOT(fct_calc()));
  QWidget::connect(charger, SIGNAL(clicked()), this, SLOT(fct_table()));
  QWidget::connect(graph, SIGNAL(clicked()), this, SLOT(fct_graph()));
  setLayout(layout);
}

void		MaFenetre::calc()
{
  if (Parseur != NULL)
    {

      std::vector<float> X = Parseur->getX();
      std::vector<float> Y = Parseur->getY();

      math = new Maths(X, Y, Parseur->getLine(), 0);

      value1[0]->setText(QString(var_to_string(math->getA(), 3).c_str()));
      value1[1]->setText(QString(var_to_string(math->getB(), 4).c_str()));
      value1[2]->setText(QString(var_to_string(math->getS(), 3).c_str()));

      math2 = new Maths(X, Y, Parseur->getLine(), 1);

      value2[0]->setText(QString(var_to_string(math2->getA(), 3).c_str()));
      value2[1]->setText(QString(var_to_string(math2->getB(), 4).c_str()));
      value2[2]->setText(QString(var_to_string(math2->getS(), 3).c_str()));

      value3->setText(QString(var_to_string(math2->getR(), 4).c_str()));

    }
  else
    QMessageBox::critical(this, "Erreur", "Veuillez charger un fichier");
}

void		MaFenetre::fct_calc()
{
  calc();
}

void		MaFenetre::fct_table()
{
  if (Parseur != NULL)
    delete Parseur;
  if (math != NULL)
    delete math;
  if (math2 != NULL)
    delete math2;

  QString fichier = QFileDialog::getOpenFileName(this, "Ouvrir un fichier", QString(), "Fichier Texte(*.txt *.ol *.fe)");

  if (fichier == NULL)
    return;

  std::string tmp = fichier.toStdString();
  Parseur = new Parse(tmp.c_str());

  if (Parseur->getY().size() != Parseur->getX().size())
    QMessageBox::critical(this, "Erreur", "Fichier incorrect");
  else
    {
      QString charg("Charger : ");
      size_t found = tmp.find_last_of("/");
      charg += tmp.substr(found+1).c_str();
      charger->setText(charg);
      for (int i = 0; i < 3; i++)
	{
	  value1[i]->setText("");
	  value2[i]->setText("");
	}
      value3->setText("");
    }
}

void	MaFenetre::fct_graph()
{
  if (Parseur != NULL)
    {

      if (math != NULL)
	{
	  SDL_opt       win(600, 600, "207 Correl");
	  math2->courbe(&win, math->getA(), math->getB(), math2->getA(), math2->getB());
	}
      else
	QMessageBox::critical(this, "Erreur", "Calculer les valeurs avant le graphique");
    }
  else
    QMessageBox::critical(this, "Erreur", "Veuillez charger un fichier");
}
